//
//  BKHomeCycleView.m
//  FastBee
//
//  Created by mac on 16/9/9.
//  Copyright © 2016年 xbk. All rights reserved.
//

#import "BKHomeCycleView.h"
#import "BKHomeCycleFlowLayout.h"
#import "BKHomeCycleCell.h"
#import "BKHomeCycleModel.h"
#import "UIView+MDRExtension.h"
#import "masonry.h"

@interface BKHomeCycleView()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic,strong) BKHomeCycleCell *cell;
@property (nonatomic,strong) NSTimer *timer;

@end
static NSString *ID = @"ID";
@implementation BKHomeCycleView

- (NSTimer *)timer{

    if (_timer == nil) {
        
        _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(playPicture) userInfo:nil repeats:YES];
        [[NSRunLoop mainRunLoop]addTimer:_timer forMode:NSRunLoopCommonModes];
    }
    return _timer;
}

- (void)playPicture{

    NSInteger page = self.num;
    NSInteger newPage = page + 1;
    CGFloat offectX = newPage *self.width;
    
    [self setContentOffset:CGPointMake(offectX, 0) animated:YES];
}


-(void)viewDidAppear:(BOOL)animated{
    
    //视图一加载就让它在第五个
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.dataArr.count inSection:0];
    
    [self scrollToItemAtIndexPath:indexPath atScrollPosition:0  animated:NO];
}

- (instancetype)initWithFrame:(CGRect)frame{

    BKHomeCycleFlowLayout *flowLayout = [[BKHomeCycleFlowLayout alloc]init];
  
    self = [super initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    
    self.dataSource = self;
    self.delegate = self;
    
    self.pagingEnabled = YES;
    self.bounces = NO;
    [self registerClass:[BKHomeCycleCell class] forCellWithReuseIdentifier:ID];
    
    [self loadCycleModelData];
    [self timer];
    
    return self;
}



//加载网络数据
- (void)loadCycleModelData{

    
    [BKHomeCycleModel downloadDataWithUrl:@"focus.json.php" successBlock:^(NSArray *arr) {
        
       // NSLog(@"%@",arr);
        self.dataArr = arr;
        [self reloadData];
        
    } failBlock:^(NSError *error) {
        
        NSLog(@"error %@",error);
    }];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.dataArr.count * 2;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    self.cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    self.cell.backgroundColor = [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0];;
    
    self.cell.model = self.dataArr[indexPath.item%self.dataArr.count];
    
    return _cell;
}

#pragma mark - UICollectionViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    //计算当前是第几个item
    NSInteger index = scrollView.contentOffset.x / scrollView.width;
    self.num = index;
    //pageControll的当前页
    //self.cell.pageControl.currentPage = index % 5;
    
    if (self.callBackBlock) {
        
        self.callBackBlock(index % 5);
    }
    
    
    //从新获取item的个数
    NSInteger items = [self numberOfItemsInSection:0];
    NSIndexPath *indexPath;
    
    if (index == 0) {
        
        indexPath = [NSIndexPath indexPathForItem:self.dataArr.count inSection:0];
        
    }else if (index == items - 1){
        
        indexPath = [NSIndexPath indexPathForItem:self.dataArr.count-1 inSection:0];
    }
    //滚动
    [self scrollToItemAtIndexPath:indexPath atScrollPosition:0 animated:NO];
}


@end
